package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 安心分期-龙岩市企诺API全量15 石家庄
 *
 * @author 李源聪
 * @since 2025年07月30日 16:38
 */
@ApiPlan(productId = "384", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskLongYanQiNuoShiJiaZhuang implements ApiOrgPhoneMaskConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.GET);
        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("file_id", 41);
        newMap.put("mobile", apiDataExchangeParameter.getPhoneMask());
        apiDataExchangeParameter.setExtendUrl("?file_id=" + 41 + "&mobile=" + apiDataExchangeParameter.getPhoneMask());
        return newMap;
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        JSONArray data = jsonObject.getJSONArray("data");
        if (ValidParam.isEmpty(data)) {
            return RespOrgPhoneMaskDataExchange.ofSuc();
        }
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        List<String> md5 = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            // 获取数组中的每个元素
            String phoneMd5 = data.getString(i);
            md5.add(phoneMd5);
        }
        respOrgPhoneMaskDataExchange.setListPhoneMd5(md5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("type", 0);
            newMap.put("file_id", 41);
            newMap.put("data", MapUtils.newHashMap(dataMap -> {
                dataMap.put("cityName", apiApplyParameter.getCity());
                dataMap.put("name", apiApplyParameter.getUserName());
                dataMap.put("phone", apiApplyParameter.getPhone());
                dataMap.put("amount", apiFieldConverter.getLoanAmountUnit());
                dataMap.put("remarks", apiFieldConverter.getExtendInfo());
            }));
        }));
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
